/**
    package: learn-go
    filename: pool
    author: diogo@gmail.com
    time: 2021/9/15 17:10
**/
package main

import (
	"log"
	"os"
	"time"

	"gitee.com/diogoxiang/learn-go/example/pool/runner"
)

func main() {
	log.Println("...开始执行任务...")

	timeout := 10 * time.Second
	r := runner.New(timeout)

	for i := 0; i <2; i++{
		r.Add(createTask(),createTask2())
	}

	if err := r.Start(); err != nil {
		switch err {
		case runner.ErrTimeOut:
			log.Println(err)
			os.Exit(1)
		case runner.ErrInterrupt:
			log.Println(err)
			os.Exit(2)
		}
	}

	log.Println("...任务执行结束...")
}

func createTask() func(int) {
	return func(id int) {
		log.Printf("正在执行任务%d", id)

		//s := make([]string, 0, 10000) // 预先设置 cap 值
		//for i := 0; i < 10000; i++ {
		//	s = append(s, approach8.RandStr(10))
		//}
		//log.Println(s[:10])
		//time.Sleep(time.Duration(id)* time.Second)
		//time.Sleep(1 * time.Millisecond)
	}
}

func createTask2() func(int) {
	return func(id int) {
		log.Printf("createTask2 正在执行任务%d", id)
	}
}